package com.itheima.d5_resource;

import java.io.*;

/*
     目标：掌握释放资源的方式：try-with-resource
 */
public class Test3 {
    public static void main(String[] args)  {
        String source = "D:\\data\\tengxun2\\usdchf_70056684.csv";
        String target = "D:\\codes\\copyfile.csv";

        long start = System.currentTimeMillis();
        copyFile(source, target);
        long end = System.currentTimeMillis();
        System.out.println("耗时：" + (end - start));
    }

    public static void copyFile(String sourceFilePath, String targetPath) {
        try (
            // 1. 创建流对象
            FileInputStream fis = new FileInputStream(sourceFilePath);
            FileOutputStream fos = new FileOutputStream(targetPath);
        ){
            // 2. 写入数据
            int len = -1;
            byte[] bytes = new byte[1024];
            while ((len = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
            }
        } catch (Exception io) {
            System.out.println("io 异常： " + io.getMessage());
        }
    }
}
